﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;

namespace TireService.Web.View.CustomerView
{
    public partial class ManageTireSet : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack != true)
            {
                if (Request.QueryString["RegNumber"] != null)
                {
                    try
                    {
                        // Set search field with car registration number
                        txtRegNumberSearchField.Text = Convert.ToString(Request.QueryString["RegNumber"]);

                        // Search for car registration number
                        SearchCarRegistrationNumber(Convert.ToString(Request.QueryString["RegNumber"]));
                    }
                    catch (Exception ex)
                    {
                        // TODO: Make exception handling
                    }
                }
            }
        }

        #region Private Functions

        /// <summary>
        /// Function that search for registration number in database
        /// </summary>
        /// <param name="RegistrationNumber">String with car registration number</param>
        private void SearchCarRegistrationNumber(string RegistrationNumber)
        {
            TireServiceDB db = new TireServiceDB();

            List<Car> cars = db.GetCarsByRegistrationNumber(RegistrationNumber);

            // No cars found
            if (cars.Count == 0)
            {
                throw new ArgumentException("Bil blev ikke fundet.");
            }

            // More than one car found
            if (cars.Count > 1)
            {
                GetCarOptions(cars);
            }

            // Only one car found
            if (cars.Count == 1)
            {
                GetCarTireSets(cars.First().CarId);
            }
        }

        /// <summary>
        /// Function that list found cars
        /// </summary>
        /// <param name="cars">List with cars</param>
        private void GetCarOptions(List<Car> cars)
        {
            // TODO: Show options for user
        }

        /// <summary>
        /// Function that list tireset for selected car
        /// </summary>
        /// <param name="CarId">Id on car</param>
        private void GetCarTireSets(int CarId)
        {
            // TODO: Show tires with actions in UI for selected car
        }

        #endregion

        #region Control Functions

        /// <summary>
        /// Button function that searches for car registration number
        /// </summary>
        protected void btnSearchTireSet_Click(object sender, EventArgs e)
        {
            try
            {
                SearchCarRegistrationNumber(Convert.ToString(txtRegNumberSearchField.Text));
            }
            catch (Exception ex)
            {
                // TODO: Make exception handling
            }
        }

        #endregion

    }
}